﻿using NpgsqlTypes;
using OSGeo.OGR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace shp2postgres.gdal
{
    public class FieldModel
    {
        public string SqlField { get; set; }
        public string GdalField { get; set; }
        public string SqlFieldType { get; set; }

        public FieldType GdalFieldType { get; set; }

        public int GdalIndex { get; set; }

        /// <summary>
        /// 导入或导出时是否需要该字段
        /// </summary>
        public bool Necessary { get; set; } = true;

        public bool IsGeom { get; set; } = false;

        public static FieldType GetGdalFieldType(String sqlFieldType)
        {
            FieldType fieldType = FieldType.OFTString;
            switch (sqlFieldType)
            {
                case "int":
                    fieldType = FieldType.OFTInteger;
                    break;
                case "bigint":
                    fieldType = FieldType.OFTInteger64;
                    break;
                case "text":
                    fieldType = FieldType.OFTString;
                    break;
                case "binary":
                    fieldType = FieldType.OFTBinary;
                    break;
                case "bit":
                    fieldType = FieldType.OFTBinary;
                    break;
                case "char":
                    fieldType = FieldType.OFTString;
                    break;
                case "datetime":
                    fieldType = FieldType.OFTDateTime;
                    break;
                case "decimal":
                    fieldType = FieldType.OFTReal;
                    break;
                case "float":
                    fieldType = FieldType.OFTReal;
                    break;
                case "nchar":
                    fieldType = FieldType.OFTString;
                    break;
                case "ntext":
                    fieldType = FieldType.OFTString;
                    break;
                case "nvarchar":
                    fieldType = FieldType.OFTString;
                    break;
                case "numeric":
                    fieldType = FieldType.OFTReal;
                    break;
                case "timestamp":
                    fieldType = FieldType.OFTTime;
                    break;
                case "varchar":
                    fieldType = FieldType.OFTString;
                    break;
                default:
                    break;
            }
            return fieldType;
        }

        public static String GetSQLFieldType(FieldType type)
        {
            String fieldType = "String";
            switch (type)
            {
                case FieldType.OFTInteger:
                    fieldType = "int";
                    break;
                case FieldType.OFTInteger64:
                    fieldType = "bigint";
                    break;
                case FieldType.OFTString:
                    fieldType = "text";
                    break;
                case FieldType.OFTBinary:
                    fieldType = "binary";
                    break;
                case FieldType.OFTDateTime:
                    fieldType = "datetime";
                    break;
                case FieldType.OFTReal:
                    fieldType = "decimal";
                    break;
                case FieldType.OFTTime:
                    fieldType = "timestamp";
                    break;
                case FieldType.OFTDate:
                    fieldType = "datetime";
                    break;
                default:
                    break;
            }
            return fieldType;
        }

        public Type Type()
        {
            Type type = typeof(string);
            switch (SqlFieldType)
            {
                case "text":
                    return typeof(string);
                case "int":
                    return typeof(int);
                case "bigint":
                    return typeof(long);
                case "binary":
                    return type;
                case "datetime":
                    return typeof(DateTime);
                case "timestamp":
                    return typeof(long);
                case "decimal":
                    return typeof(decimal);
                case "wkbPolygon":
                    return typeof(byte[]);
                case "wkbPoint":
                    return typeof(byte[]);
                case "wkbMultiPoint":
                    return typeof(byte[]);
                case "wkbMultiPolygon":
                    return typeof(byte[]);
                case "wkbLineString":
                    return typeof(byte[]);
                case "wkbMultiLineString":
                    return typeof(byte[]);
                default:
                    break;
            }
            return type;
        }
    }
}
